//
//  SignView.m
//  xingfugangwan
//
//  Created by rainday on 14/12/28.
//  Copyright (c) 2014年 rainday. All rights reserved.
//

#import "SignView.h"

@implementation SignView


- (id)initWithCoder:(NSCoder *)aDecoder
{
    
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _path = [UIBezierPath bezierPath];
        
        // 捕捉触摸
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        pan.maximumNumberOfTouches = pan.minimumNumberOfTouches = 1;
        [self addGestureRecognizer:pan];
    }
    return self;
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [[UIColor blackColor] setStroke];
    [_path setLineWidth:5.0];
    [_path stroke];
}

- (void)pan:(UIPanGestureRecognizer *)pan {
    CGPoint currentPoint = [pan locationInView:self];
    CGPoint midPoint = midpoint(_previousPoint, currentPoint);
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        [_path moveToPoint:currentPoint];
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        [_path addQuadCurveToPoint:midPoint controlPoint:_previousPoint];
    }
    
    _previousPoint = currentPoint;
    
    [self setNeedsDisplay];
}

static CGPoint midpoint(CGPoint p0, CGPoint p1) {
    return (CGPoint) {
        (p0.x + p1.x) / 2.0,
        (p0.y + p1.y) / 2.0
    };
}
@end
